package com.mcxx.modules.minimumliving.controller;

import com.mcxx.modules.minimumliving.service.SingleFundPayService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.fundpay.dto.SingleRollDTO;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayFamilyParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayListSearchParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayParam;
import com.mcxx.modules.miniSalvation.fundpay.service.read.SingleFundPayReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.write.SingleFundPayWriteService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Validated
@Api("含单人保逻辑发放管理")
@RestService({"minimumLiving/singleFundPay/v2"})
public class SingleFundPayController extends DefaultRestService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SingleFundPayController.class);

    @Autowired
    private SingleFundPayReadService singleFundPayReadService;

    @Autowired
    private SingleFundPayService singleFundPayService;

    @Autowired
    private SingleFundPayWriteService singleFundPayWriteService;

    @Value("${society.fundpay.delUrl}")
    private String fundpayDelUrl;




    @PostMapping("getSingleCapitalFundPayList")
    @ApiOperation("获取保障金发放名册")
    public void getSingleCapitalFundPayList(FundPayListSearchParam fundPayListSearchParam, PageParam pageParam) {
        LOGGER.info("查询获取保障金发放名册,参数：{}", fundPayListSearchParam);

        fundPayListSearchParam.setBusinessType(Constant.BusinessCode.DIBAO);
        Page page = singleFundPayReadService.getFundPayList(fundPayListSearchParam, pageParam, getCurUserAccount());
        this.setPageBean(page);
        LOGGER.info("查询获取保障金发放名册,结果：{}", page);
    }


    @PostMapping("addSingleCapitalFundPay")
    @ApiOperation("创建保障金发放名册")
    public void addSingleCapitalFundPay(SingleRollDTO params) {
        LOGGER.info("创建保障金一卡通发放名册,参数：{}", params);
        UserAccountVo user = this.getCurUserAccount();
        params.setAreaCode(null);
        QueryParamUtil.setBaseParamValue(params, user);
        params.setOrgId(user.getOrgId());

        params.setBusinessType(Constant.BusinessCode.DIBAO);

        params.setFundType(Constant.FundType.CAPITAL_FUND_PAY);

        params.setPayType(Constant.FundType.CAPITAL_FUND_PAY);

        singleFundPayService.saveSingCapitalFundPay(params, user);
        setData(true);
    }


    @PostMapping("verifyCapitalStatRollIsCreate")
    @ApiOperation("创建保障金发放名册")
    public void verifyCapitalStatRollIsCreate(SingleRollDTO capitalFundPayDTO) {
        List<String> flag = singleFundPayReadService.verifyHasCreateStatRoll(capitalFundPayDTO);
        setData(flag.size() == 0);
    }


    @PostMapping("delSingleCapitalFundPay")
    @ApiOperation("删除保障金发放名册")
    public void delSingleCapitalFundPay(@V("notnull") @RequestPart List<String> fundPayIdList) {
        LOGGER.info("删除保障金发放名册,id：{}", fundPayIdList);

        BaseResult<Map<String, List<String>>> result = singleFundPayWriteService.delFundPay(fundPayIdList, fundpayDelUrl);
        if (result.isFail()) {
            setData(result.getMsg());
            LOGGER.info("删除保障金发放名册失败：" + result.getMsg());
            return;
        }
        Map<String, List<String>> rstMap = result.getData();

        setData(packageRstMsg(rstMap));
        LOGGER.info("删除保障金发放名册,结果：{}", rstMap);
    }


    private String packageRstMsg(Map<String, List<String>> rstMap) {
        StringBuffer rstMsg = new StringBuffer();
        if (rstMap.containsKey("success")) {
            rstMsg.append("成功【").append(rstMap.get("success").size()).append("】条\n");
        }
        if (rstMap.containsKey("fail")) {
            rstMsg.append("失败【").append(rstMap.get("fail").size()).append("】条");
        }
        return rstMsg.toString();
    }


    @PostMapping("verifyOrderNoticeState")
    @ApiOperation("验证保障金发放名册中是否存在未结束公示的")
    public void verifyOrderNoticeState(@V("notnull") String fundPayId) {
        LOGGER.info("验证保障金发放名册中是否存在未结束公示的,id：{}", fundPayId);
        int rst = singleFundPayService.verifyNoCompleteNotice(fundPayId);
        setData(rst);
        LOGGER.info("验证保障金发放名册中是否存在未结束公示的,业务条数：{}", rst);
    }


    @PostMapping("confirmCapitalFundPay")
    @ApiOperation("将保障金发放名册状态修改为已确认")
    public void confirmCapitalFundPay(@V("notnull") String fundPayId) {
        LOGGER.info("确认保障金名册,id：{}", fundPayId);
        singleFundPayService.confirmFundPay(fundPayId, getCurUserAccount());
        LOGGER.info("确认保障金名册,结果：{}", true);
    }


    @PostMapping("getSingleCapitalDetailList")
    @ApiOperation("查询-保障金发放名册列表明细")
    public void getSingleCapitalDetailList(@V("notnull") SingleRollDTO fundPayFamilyParam, @V("notnull") PageParam pageParam) {
        LOGGER.info("查看-保障金发放名册明细,参数：{}", fundPayFamilyParam);
        fundPayFamilyParam.setFundType(Constant.FundType.CAPITAL_FUND_PAY);

        fundPayFamilyParam.setBusinessType(Constant.BusinessCode.DIBAO);

        Page page = singleFundPayReadService.getFundPayFamilyList(fundPayFamilyParam, pageParam, getCurUserAccount());
        this.setPageBean(page);
        LOGGER.info("查看-保障金发放名册明细,结果：{}", page);
    }


    @PostMapping("cancelCapitalFundPay")
    @ApiOperation("将保障金发放名册修改为撤销")
    @Deprecated
    public void cancelCapitalFundPay(@V("notnull") @RequestPart List<String> fundPayIdList) {
        LOGGER.info("撤销保障金发放名册,id：{}", fundPayIdList);

        BaseResult<Map<String, List<String>>> result = singleFundPayService.cancelFundPay(fundPayIdList);
        if (result.isFail()) {
            setData(result.getMsg());
            return;
        }
        Map<String, List<String>> rstMap = result.getData();

        setData(packageRstMsg(rstMap));
        LOGGER.info("撤销保障金发放名册,结果：{}", rstMap);
    }


    @PostMapping("getSingleFundPayExceptionList")
    @ApiOperation("查看该名册异常数据")
    public void getSingleFundPayExceptionList(@V("notnull") String fundPayId, PageParam pageParam) {
        LOGGER.info("根据名册查看该名册生成过程中是否存在异常情况,id：{}", fundPayId);
        Page page = singleFundPayService.getPageFundPayException(fundPayId, pageParam);
        LOGGER.info("结果：" + page.getPageSize());
        setPageBean(page);
    }


    @PostMapping("verifySyncFundPay")
    @ApiOperation("检查发放名册是否可以同步数据")
    public void verifySyncFundPay(@V("notnull") String fundPayId) {
        LOGGER.info("同步发放名册至财政发放系统前，检查发放名册是否可以同步数据到发放系统,id：{}", fundPayId);
        String msg = singleFundPayService.checkCanSyncFundPayData(fundPayId);
        LOGGER.info("结果：" + msg);
        setData(msg);
    }


    @PostMapping("syncFundPayData")
    @ApiOperation("同步发放名册至财政发放系统")
    public void syncFundPayData(@V("notnull") String fundPayId) {
        LOGGER.info("同步发放名册至财政发放系统,id：{}", fundPayId);
        String msg = singleFundPayService.syncFundPayData(fundPayId);
        LOGGER.info("结果：" + msg);
        setData(msg);
    }


    @PostMapping({"exportFundPayRoll"})
    public void exportFundPayRoll(HttpServletResponse response, @RequestBody FundPayParam param) throws IOException {
        ExcelInfo excelInfo = singleFundPayService.exportFundPayRoll(param.getExportType(), param.getIdsList(), param, getCurUserAccount());
        if (null != excelInfo) {
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @PostMapping({"exportFundPayRollDetail"})
    public void exportFundPayRollDetail(HttpServletResponse response, @RequestBody FundPayFamilyParam param) throws IOException {
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        ExcelInfo excelInfo = singleFundPayService.exportFundPayRollDetail(param, getCurUserAccount());
        if (null != excelInfo) {
            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }




    @PostMapping("addFundPayRoll")
    @ApiOperation("创建标准补发名册")
    public void addFundPayRoll(@V("notnull") SingleRollDTO singleRollDTO) {
        LOGGER.info("创建标准补发名册,参数：{}", singleRollDTO);
        UserAccountVo userAccountVo = this.getCurUserAccount();
        singleRollDTO.setAreaCode(null);
        QueryParamUtil.setBaseParamValue(singleRollDTO, userAccountVo);
        singleRollDTO.setOrgId(userAccountVo.getOrgId());

        singleRollDTO.setBusinessType(Constant.BusinessCode.DIBAO);

        singleRollDTO.setPayType(Constant.FundType.STAND_FUND_PAY);

        singleFundPayService.addFundPayBiz(singleRollDTO, userAccountVo);


        setData(true);
        LOGGER.info("创建标准补发名册，结果是：{}", true);
    }



}
